﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyCocos2D
{
    public class CCScene
    {
        public CCScene()
        {
        }

        public string LibH()
        {
            return "#include \"cocos2d.h\"\n#include \"cocos-ext.h\"\n";
        }

        public string LibCPP(string libName)
        {
            string lib = "";
            lib = String.Format("#include \"{0}.h\"\n", libName);
            return lib;
        }

        public string AfferLib()
        {
            return "\nUSING_NS_CC;\nUSING_NS_CC_EXT;\n\n";
        }

        public string Constructor(string sceneName)
        {
            string s = "";
            s = string.Format("CCScene* {0}::scene()\n{{\n\tCCScene *scene = CCScene::create();\n\t{0} *layer = {0}::create();\n\tscene->addChild(layer);\n\treturn scene;\n}}\n\n", sceneName);
            return s;
        }

        public string InitH(string sceneName, string body)
        {
            string init = "";
            init += string.Format("class {0} : public cocos2d::CCLayerColor\n{{\n\tpublic:\n\t\tvirtual bool init();\n\t\tstatic cocos2d::CCScene* scene();\n{1}\n}}", sceneName, body);
            return init;
        }

        public string InitCPP(string sceneName, string body)
        {
            string init = "";
            init += string.Format("bool {0}::init()\n", sceneName);
            init += "{\n";
            init += string.Format("{0}", body);
            init += "\n\treturn true;\n}";
            return init;
        }

        public string InitBodyH(List<CCGameObject> lstgameObj)
        {
            string s = "";

            foreach (CCGameObject gameobj in lstgameObj)
            {
                s += gameobj.serialineH();
            }

            return s;
        }

        public string InitBodyCPP(List<CCGameObject> lstgameObj)
        {
            string s = "";

            foreach (CCGameObject gameobj in lstgameObj)
            {
                s += gameobj.serialineCPP();
                s += string.Format("\tthis->addChild({0});\n\n", gameobj.Name);
            }

            return s;
        }

        public string SumH(string sceneName, List<CCGameObject> lstgameObj)
        {
            string sum = "";
            sum += LibH() + AfferLib() + InitH(sceneName, InitBodyH(lstgameObj));
            return sum;
        }

        public string SumCPP(string sceneName, List<CCGameObject> lstgameObj)
        {
            string sum = "";
            sum += LibCPP(sceneName) + AfferLib() + Constructor(sceneName) + InitCPP(sceneName, InitBodyCPP(lstgameObj));
            return sum;
        }

    }
}
